package cac.fenjian.resource.msfactory.service;

import cac.fenjian.resource.MsFactory.dto.MsFactoryDTO;
import cac.fenjian.resource.msfactory.dao.MsFactoryDao;
import com.cac.dme.adapter.rest.msg.QueryReqBean;
import com.cac.dme.adapter.utils.ComUtil;
import com.cac.dme.adapter.utils.PojoUtil;
import com.cac.dme.common.exception.service.DaoException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @program test-curd
 * @description 专业厂配置服务
 * @author: LSL
 * @create: 2022-07-11-16-23
 **/
@Service
public class MsFactoryService {
    @Autowired
    private MsFactoryDao msFactoryDao;

    /**
     * 新增一条数据
     * @param msFactoryDTO
     * @return
     */
    @Transactional
    public String  insertMsFactoryDTO(MsFactoryDTO msFactoryDTO){
        try{
            msFactoryDTO.setMsFactoryId(ComUtil.getId());
            msFactoryDao.insertMsFactoryDTO(msFactoryDTO);
            return msFactoryDTO.getMsFactoryId();
        }catch (Exception e){
            throw new DaoException(e.getMessage(),e);
        }
    }

    /**
     * 批量新增数据
     * @param dtoList
     * @return
     */
    @Transactional
    public Integer insertBatchMsFactoryDTO(List<MsFactoryDTO> dtoList){
        List<MsFactoryDTO> beanList = new ArrayList<>();
        for (MsFactoryDTO msFactoryDTO: dtoList
        ) {
            msFactoryDTO.setMsFactoryId(ComUtil.getId());
            beanList.add(msFactoryDTO);
        }
        try{
            return msFactoryDao.insertBatchMsFactoryDTO(beanList);
        }catch (Exception e){
            throw new DaoException(e.getMessage(),e);
        }
    }

    /**
     * 根据条件查询数据
     * @param queryReqBean
     * @return
     */
    @Transactional
    public List<MsFactoryDTO> searchByParams(QueryReqBean<MsFactoryDTO> queryReqBean){
        try{
            return msFactoryDao.searchByParams(queryReqBean.getSearchParams());
        }catch (Exception e){
            throw new DaoException(e.getMessage(),e);
        }
    }

    /**
     * 修改数据信息
     * @param msFactoryDTO
     * @return
     */
    public Integer updateDate(MsFactoryDTO msFactoryDTO){
        try{
            int count = msFactoryDao.updateMsFactoryDTO(getUpdateDto(msFactoryDTO));
            if(count==0){
                throw new DaoException("数据修改失败");
            }
            return count;
        }catch (Exception e){
            throw new DaoException(e.getMessage(),e);
        }
    }

    /**
     * 根据ID删除数据
     * @param id
     * @return
     */
    public Integer deleteDate(String id ){
        if(StringUtils.isEmpty(id)){
            throw  new DaoException("参数为空，删除失败");
        }
        try{
            if(msFactoryDao.searchById(id)==null){
                throw  new DaoException("数据为空，删除失败");
            }
            int count = msFactoryDao.deleteMsFactoryDTO(id);
            return count;
        }catch (Exception e){
            throw new DaoException(e.getMessage(),e);
        }
    }

    /**
     * 封装方法
     * @param msFactoryDTO
     * @return
     */
    private MsFactoryDTO getUpdateDto(MsFactoryDTO msFactoryDTO) {
        MsFactoryDTO oldDto = msFactoryDao.searchById(msFactoryDTO.getMsFactoryId());
        if(oldDto == null){
            throw  new DaoException("数据不存在");
        }
        //新数据信息复制到旧数据信息中
        PojoUtil.copyProperties(oldDto,msFactoryDTO,true);
        return oldDto;
    }
}
